@using Microsoft.AspNetCore.Html
@using OrchardCore.Modules;
@model FeaturesViewModel

@{
    //var lifecycleStatusClassDictionary = new Dictionary<LifecycleStatus, string> {
    //    { LifecycleStatus.Production, "fa-check" },
    //    { LifecycleStatus.Preview, "fa-info-circle" },
    //    { LifecycleStatus.Deprecated, "fa-exclamation-circle" }
    //};
}
<h1>@RenderTitleSegments(T["Features"])</h1>

<nav class="admin-toolbar">
    <div class="nav justify-content-end">
        @*
            <fieldset class="form-group">
                @Html.Hidden("force", true)
                @Html.Hidden("featureIds")
                <input id="bulkActions" name="bulkAction" type="hidden" value="@FeaturesBulkAction.None" />
                <button name="submit.BulkExecute" type="submit" class="hidden" value="yes">@T["Execute"]</button>
                <div class="btn-group">
                    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
                        <i class="fa fa-bolt"></i> @T["Bulk Actions"] <span class="caret"></span>
                    </button>&nbsp;
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#" data-bulk-action-confirm="@T["Are you sure you want to enable these items?"]" data-bulk-action="@FeaturesBulkAction.Enable">@T["Enable"]</a></li>
                        <li><a href="#" data-bulk-action-confirm="@T["Are you sure you want to disable these items? "]" data -bulk-action="@FeaturesBulkAction.Disable">@T["Disable"]</a></li>
                        <li><a href="#" data-bulk-action-confirm="@T["Are you sure you want to update these items? "]" data -bulk-action="@FeaturesBulkAction.Update">@T["Update"]</a></li>
                        <li><a href="#" data-bulk-action-confirm="@T["Are you sure you want to toggle these items? "]" data -bulk-action="@FeaturesBulkAction.Toggle">@T["Toggle"]</a></li>
                    </ul>
                </div>
                <select id="publishActions" name="bulkAction">
                    <option value="@FeaturesBulkAction.None">@T["Choose action..."]</option>
                    <option value="@FeaturesBulkAction.Enable">@T["Enable"]</option>
                    <option value="@FeaturesBulkAction.Disable">@T["Disable"]</option>
                    <option value="@FeaturesBulkAction.Toggle">@T["Toggle"]</option>
                </select>
                <button type="submit" name="submit.BulkExecute" value="yes" class="btn-primary">@T["Execute"]</button>
            </fieldset>
        *@

        <form class="form-inline float-right" autocomplete="off" method="get">
            <input id="search-box" class="form-control" type="text" placeholder="@T["Search"]" autofocus="autofocus">
        </form>
    </div>
</nav>

<form asp-action="Features">
    @{
        var featureGroups = Model.Features.GroupBy(f => f.Descriptor.Category).ToList(); //.OrderBy(f => f.Descriptor.Category, new DoghouseComparer("Core"))
        foreach (var featureGroup in featureGroups)
        {
            if (!featureGroup.Any(x => Model.IsAllowed(x.Descriptor)))
            {
                continue;
            }
            var categoryName = featureGroup.First().Descriptor.Category ?? T["Uncategorized"].Value; //LocalizedString.TextOrDefault(featureGroup.First().Descriptor.Category, T("Uncategorized"));
            var categoryClassName = string.Format("category {0}", Html.Encode(categoryName.ToString())); //Html.Encode(categoryName.ToString().HtmlClassify()));
            if (featureGroup == featureGroups.First())
            {
                categoryClassName += " first";
            }
            if (featureGroup == featureGroups.Last())
            {
                categoryClassName += " last";
            }

            <div class="feature-group">
                @{
                    var features = featureGroup.OrderBy(f => f.Descriptor.Name);
                    var categoryFeatureNames = String.Join(" ", features.Select(f => f.Descriptor.Name));
                }
                <h2 class="mb-2 mt-3" data-filter-value="@categoryName @categoryFeatureNames">@categoryName</h2>
                <ul class="list-group" data-filter-value="@categoryName @categoryFeatureNames">
                    @{
                        foreach (var feature in features)
                        {
                            if (!Model.IsAllowed(feature.Descriptor))
                            {
                                continue;
                            }

                            //var lifecycleStatus = feature.Descriptor.Extension.LifecycleStatus;
                            var featureId = feature.Descriptor.Id;
                            var featureName = feature.Descriptor.Name;
                            var featureState = feature.IsEnabled ? "enabled" : "disabled";

                            var dependencies = (from d in feature.FeatureDependencies
                                                select (from f in Model.Features where f.Descriptor.Id == d.Id select f).SingleOrDefault()).Where(f => f != null).OrderBy(f => f.Descriptor.Name);
                            var missingDependencies = feature.FeatureDependencies
                                .Where(d => !Model.Features.Any(f => f.Descriptor.Id == d.Id));
                            var showDisable = !feature.IsAlwaysEnabled && categoryName != "Core" && feature.Descriptor.Name != Application.ModuleName;
                            var showEnable = Model.IsAllowed(feature.Descriptor) && !missingDependencies.Any() && feature.Descriptor.Id != "OrchardCore.Setup";

                            <li class="list-group-item" data-filter-value="@categoryName @featureName">
                                <div class="row">
                                    <div class="col-md-10">
                                        @* Display the checkbox ?*@
                                        @if ((showEnable && !feature.IsEnabled) || (showDisable && feature.IsEnabled))
                                        {
                                            <div class="custom-control custom-checkbox">
                                                <input type="checkbox" class="custom-control-input" name="featureIds" id="@feature.Descriptor.Id" value="@feature.Descriptor.Id">
                                                <label class="custom-control-label h5" for="@feature.Descriptor.Id">@featureName</label>
                                            </div>
                                        }
                                        else
                                        {
                                            <h5>@featureName</h5>
                                        }

                                        <p class="text-muted">@feature.Descriptor.Description</p>
                                        <div>
                                            @if (feature.FeatureDependencies != null && feature.FeatureDependencies.Any())
                                            {
                                                @foreach (var d in dependencies)
                                                {
                                                    <span class="badge badge-info">
                                                        @(string.IsNullOrEmpty(d.Descriptor.Name) ? d.Descriptor.Id : d.Descriptor.Name)
                                                    </span>
                                                }
                                                @foreach (var d in missingDependencies)
                                                {
                                                    <span class="badge badge-warning">
                                                        @d
                                                    </span>
                                                }
                                            }
                                        </div>
                                    </div>
                                    <div class="col-md-2 text-right">
                                        @if (showEnable && !feature.IsEnabled)
                                        {
                                            <a asp-action="Enable" asp-route-id="@feature.Descriptor.Id" class="btn btn-primary btn-sm" itemprop="UnsafeUrl">@T["Enable"]</a>
                                        }
                                        @if (showDisable && feature.IsEnabled)
                                        {
                                            var confirmationMessage = T["Are you sure you want to disable this feature? Continue?"];
                                            if (feature.DependentFeatures.Any())
                                            {
                                                var dependentFeatures = new HtmlString(String.Join(" ", feature.DependentFeatures.Select(f => f.Name)));
                                                confirmationMessage = T["Disabling this feature will also disable the following dependent features: {0}. Continue?", dependentFeatures];
                                            }
                                            <a asp-action="Disable" asp-route-id="@feature.Descriptor.Id" class="btn btn-danger btn-sm" data-message="@confirmationMessage" itemprop="UnsafeUrl RemoveUrl">@T["Disable"]</a>
                                        }
                                    </div>
                                </div>
                            </li>
                        }
                    }
                </ul>
            </div>
        }
    }

    @*<li class="@featureClassName" id="@featureId" title="@string.Format(feature.IsEnabled ? "{0} is enabled" : "{0} is disabled", featureName)">
            <div class="panel panel-default panel-heading-fullwidth panel-borders">
                <div class="panel-heading">
                    <div class="title">
                        @if ((showEnable && !feature.IsEnabled) || (showDisable && feature.IsEnabled))
                        {
                            <span class="or-checkbox">
                                <input type="checkbox" name="featureIds" id="@feature.Descriptor.Id" value="@feature.Descriptor.Id" />
                                <label for="@feature.Descriptor.Id">
                                    @featureName
                                </label>
                            </span>
                        }
                        else
                        {
                            @featureName
                        }
                    </div>
                    <div class="tools">
                        @if (showDisable && feature.IsEnabled)
                        {
                            var dependantsJoined = string.Join(", ", feature.DependentFeatures.Select(f => f.Name));
                            <div class="switch-button">
                                <input type="checkbox" checked="" name="swt@(feature.Descriptor.Id)" id="swt@(feature.Descriptor.Id)"><span>
                                    <label for="swt@(feature.Descriptor.Id)" data-feature-id="@feature.Descriptor.Id" data-feature-action="@FeaturesBulkAction.Disable" data-feature-force="true" data-feature-dependants="@dependantsJoined">@T["Disable"]</label>
                                </span>
                            </div>
                        }

                        @if (showEnable && !feature.IsEnabled)
                        {
                            <div class="switch-button">
                                <input type="checkbox" name="swt@(feature.Descriptor.Id)" id="swt@(feature.Descriptor.Id)"><span>
                                    <label for="swt@(feature.Descriptor.Id)" data-feature-id="@feature.Descriptor.Id" data-feature-action="@FeaturesBulkAction.Enable" data-feature-force="true">@T["Enable"]</label>
                                </span>
                            </div>
                        }

                        @if (feature.NeedsUpdate)
                        {
                            <span class="icon s7-upload">
                                <a href="#" data-feature-id="@feature.Descriptor.Id" data-feature-action="@FeaturesBulkAction.Update" data-feature-force="false">@T["Update"]</a>
                            </span>
                        }
                    </div>
                </div>
                <div class="panel-body">
                    <p class="description" title="@feature.Descriptor.Description">@feature.Descriptor.Description</p>
                    @if (feature.Descriptor.Dependencies != null && feature.Descriptor.Dependencies.Any())
                    {
                        <div class="dependencies">
                            <h4>@T["Depends on:"]</h4>
                            <ul>
                                @foreach (var d in dependencies)
                                {
                                    <li>
                                        <a href="#@d">@(string.IsNullOrEmpty(d.Descriptor.Name) ? d.Descriptor.Id : d.Descriptor.Name)</a>
                                    </li>
                                }
                            </ul>
                        </div>
                    }
                    @if (missingDependencies.Any())
                    {
                        <div class="missingdependencies">
                            <h4>@T["Missing:"]</h4>
                            <ul>
                                @foreach (var d in missingDependencies)
                                {
                                    <li>
                                        @d
                                    </li>
                                }
                            </ul>
                        </div>}
                </div>
                <div class="panel-footer">
                        @if (lifecycleStatus != LifecycleStatus.Production) {
                            var iconClass = lifecycleStatusClassDictionary[lifecycleStatus];
                            <div class="lifecycle-status lifecycle-status-@feature.Descriptor.Extension.LifecycleStatus.ToString().HtmlClassify()">
                                <i class="fa @iconClass"></i>
                                @T(lifecycleStatus.ToString())
                            </div>
                        }
                    </div>
            </div>
        </li>*@
</form>
<script at="Foot" type="text/javascript">
    //<![CDATA[
    $(function () {
        var searchBox = $('#search-box');

        // On each keypress filter the list of features
        searchBox.keyup(function (e) {
            var search = $(this).val().toLowerCase();
            var elementsToFilter = $("[data-filter-value]");

            // On ESC, clear the search box and display all features
            if (e.keyCode == 27 || search == '') {
                searchBox.val('');
                elementsToFilter.toggle(true);
            } else {
                elementsToFilter.each(function () {
                    var text = $(this).data('filter-value').toLowerCase();
                    var found = text.indexOf(search) > -1;
                    $(this).toggle(found);
                });
            }
        });
    });
    //]]>
</script>
